home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / src / binutils.252 / gas / stabs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-23  |  9.1 KB  |  384 lines

  1. /* Generic stabs parsing for gas.
  2.    Copyright (C) 1989, 1990, 1991, 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of GAS, the GNU Assembler.
  5.  
  6. GAS is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as
  8. published by the Free Software Foundation; either version 2,
  9. or (at your option) any later version.
  10.  
  11. GAS is distributed in the hope that it will be useful, but
  12. WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
  14. the GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public
  17. License along with GAS; see the file COPYING.  If not, write
  18. to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  19.  
  20. #include "as.h"
  21. #include "obstack.h"
  22. #include "subsegs.h"
  23.  
  24. /* We need this, despite the apparent object format dependency, since
  25.    it defines stab types, which all object formats can use now. */
  26.  
  27. #include "aout/stab_gnu.h"
  28.  
  29. /* Allow backends to override the names used for the stab sections.  */
  30. #ifndef STAB_SECTION_NAME
  31. #define STAB_SECTION_NAME ".stab"
  32. #endif
  33.  
  34. #ifndef STAB_STRING_SECTION_NAME
  35. #define STAB_STRING_SECTION_NAME ".stabstr"
  36. #endif
  37.  
  38. /*
  39.  * Handle .stabX directives, which used to be open-coded.
  40.  * So much creeping featurism overloaded the semantics that we decided
  41.  * to put all .stabX thinking in one place. Here.
  42.  *
  43.  * We try to make any .stabX directive legal. Other people's AS will often
  44.  * do assembly-time consistency checks: eg assigning meaning to n_type bits
  45.  * and "protecting" you from setting them to certain values. (They also zero
  46.  * certain bits before emitting symbols. Tut tut.)
  47.  *
  48.  * If an expression is not absolute we either gripe or use the relocation
  49.  * information. Other people's assemblers silently forget information they
  50.  * don't need and invent information they need that you didn't supply.
  51.  */
  52.  
  53. /*
  54.  * Build a string dictionary entry for a .stabX symbol.
  55.  * The symbol is added to the .<secname>str section.
  56.  */
  57.  
  58. #ifdef SEPARATE_STAB_SECTIONS
  59.  
  60. unsigned int
  61. get_stab_string_offset (string, stabstr_secname)
  62.      const char *string;
  63.      const char *stabstr_secname;
  64. {
  65.   unsigned int length;
  66.   unsigned int retval;
  67.  
  68.   retval = 0;
  69.   length = strlen (string);
  70.   if (length > 0)
  71.     {                /* Ordinary case. */
  72.       segT save_seg;
  73.       subsegT save_subseg;
  74.       char *newsecname;
  75.       segT seg;
  76.       char *p;
  77.  
  78.       save_seg = now_seg;
  79.       save_subseg = now_subseg;
  80.  
  81.       /* Create the stab string section.  */
  82.       newsecname = xmalloc ((unsigned long) (strlen (stabstr_secname) + 1));
  83.       strcpy (newsecname, stabstr_secname);
  84.  
  85.       seg = subseg_new (newsecname, 0);
  86.  
  87.       retval = seg_info (seg)->stabu.stab_string_size;
  88.       if (retval > 0)
  89.     free (newsecname);
  90.       else
  91.     {
  92.       /* Make sure the first string is empty.  */
  93.       p = frag_more (1);
  94.       *p = 0;
  95.       retval = seg_info (seg)->stabu.stab_string_size = 1;
  96. #ifdef BFD_ASSEMBLER
  97.       bfd_set_section_flags (stdoutput, seg, SEC_READONLY | SEC_DEBUGGING);
  98. #else
  99.       free (newsecname);
  100. #endif
  101.     }
  102.  
  103.       p = frag_more (length + 1);
  104.       strcpy (p, string);
  105.  
  106.       seg_info (seg)->stabu.stab_string_size += length + 1;
  107.  
  108.       subseg_set (save_seg, save_subseg);
  109.     }
  110.  
  111.   return retval;
  112. }
  113.  
  114. #endif /* SEPARATE_STAB_SECTIONS */
  115.  
  116. /* This can handle different kinds of stabs (s,n,d) and different
  117.    kinds of stab sections. */
  118.  
  119. static void 
  120. s_stab_generic (what, stab_secname, stabstr_secname)
  121.      int what;
  122.      char *stab_secname;
  123.      char *stabstr_secname;
  124. {
  125.   long longint;
  126.   char *string;
  127.   int type;
  128.   int other;
  129.   int desc;
  130.  
  131.   /* The general format is:
  132.      .stabs "STRING",TYPE,OTHER,DESC,VALUE
  133.      .stabn TYPE,OTHER,DESC,VALUE
  134.      .stabd TYPE,OTHER,DESC
  135.      At this point input_line_pointer points after the pseudo-op and
  136.      any trailing whitespace.  The argument what is one of 's', 'n' or
  137.      'd' indicating which type of .stab this is.  */
  138.  
  139.   if (what != 's')
  140.     string = "";
  141.   else
  142.     {
  143.       int length;
  144.  
  145.       string = demand_copy_C_string (&length);
  146.       SKIP_WHITESPACE ();
  147.       if (*input_line_pointer == ',')
  148.     input_line_pointer++;
  149.       else
  150.     {
  151.       as_warn (".stabs: Missing comma");
  152.       ignore_rest_of_line ();
  153.       return;
  154.     }
  155.     }
  156.  
  157.   if (get_absolute_expression_and_terminator (&longint) != ',')
  158.     {
  159.       as_warn (".stab%c: Missing comma", what);
  160.       ignore_rest_of_line ();
  161.       return;
  162.     }
  163.   type = longint;
  164.  
  165.   if (get_absolute_expression_and_terminator (&longint) != ',')
  166.     {
  167.       as_warn (".stab%c: Missing comma", what);
  168.       ignore_rest_of_line ();
  169.       return;
  170.     }
  171.   other = longint;
  172.  
  173.   desc = get_absolute_expression ();
  174.   if (what == 's' || what == 'n')
  175.     {
  176.       if (*input_line_pointer != ',')
  177.     {
  178.       as_warn (".stab%c: Missing comma", what);
  179.       ignore_rest_of_line ();
  180.       return;
  181.     }
  182.       input_line_pointer++;
  183.       SKIP_WHITESPACE ();
  184.     }
  185.  
  186.   /* We have now gathered the type, other, and desc information.  For
  187.      .stabs or .stabn, input_line_pointer is now pointing at the
  188.      value.  */
  189.  
  190. #ifdef SEPARATE_STAB_SECTIONS
  191.   /* Output the stab information in a separate section.  This is used
  192.      at least for COFF and ELF.  */
  193.   {
  194.     segT saved_seg = now_seg;
  195.     subsegT saved_subseg = now_subseg;
  196.     fragS *saved_frag = frag_now;
  197.     valueT dot;
  198.     segT seg;
  199.     unsigned int stroff;
  200.     char *p;
  201.     
  202.     dot = frag_now_fix ();
  203.  
  204.     seg = subseg_new (stab_secname, 0);
  205.  
  206.     if (! seg_info (seg)->hadone)
  207.       {
  208. #ifdef BFD_ASSEMBLER
  209.     bfd_set_section_flags (stdoutput, seg,
  210.                    SEC_READONLY | SEC_RELOC | SEC_DEBUGGING);
  211. #endif
  212. #ifdef INIT_STAB_SECTION
  213.     INIT_STAB_SECTION (seg);
  214. #endif
  215.     seg_info (seg)->hadone = 1;
  216.       }
  217.  
  218.     stroff = get_stab_string_offset (string, stabstr_secname);
  219.  
  220.     /* At least for now, stabs in a special stab section are always
  221.        output as 12 byte blocks of information.  */
  222.     p = frag_more (8);
  223.     md_number_to_chars (p, (valueT) stroff, 4);
  224.     md_number_to_chars (p + 4, (valueT) type, 1);
  225.     md_number_to_chars (p + 5, (valueT) other, 1);
  226.     md_number_to_chars (p + 6, (valueT) desc, 2);
  227.  
  228.     if (what == 's' || what == 'n')
  229.       {
  230.     /* Pick up the value from the input line.  */
  231.     cons (4);
  232.     input_line_pointer--;
  233.       }
  234.     else
  235.       {
  236.     const char *fake;
  237.     symbolS *symbol;
  238.     expressionS exp;
  239.  
  240.     /* Arrange for a value representing the current location.  */
  241.     fake = FAKE_LABEL_NAME;
  242.     symbol = symbol_new (fake, saved_seg, dot, saved_frag);
  243.  
  244.     exp.X_op = O_symbol;
  245.     exp.X_add_symbol = symbol;
  246.     exp.X_add_number = 0;
  247.  
  248.     emit_expr (&exp, 4);
  249.       }
  250.  
  251. #ifdef OBJ_PROCESS_STAB
  252.     OBJ_PROCESS_STAB (seg, string, stroff, type, other, desc);
  253. #endif
  254.  
  255.     subseg_set (saved_seg, saved_subseg);
  256.   }
  257. #else /* ! SEPARATE_STAB_SECTIONS */
  258. #ifdef OBJ_PROCESS_STAB
  259.   OBJ_PROCESS_STAB (what, string, type, other, desc);
  260. #else
  261.   /* Put the stab information in the symbol table.  */
  262.   {
  263.     symbolS *symbol;
  264.  
  265.     symbol = symbol_new (string, undefined_section, 0,
  266.              (struct frag *) NULL);
  267.     if (what == 's' || what == 'n')
  268.       {
  269.     /* Pick up the value from the input line.  */
  270.     symbol->sy_frag = &zero_address_frag;
  271.     pseudo_set (symbol);
  272.       }
  273.     else
  274.       {
  275.     /* .stabd sets the name to NULL.  Why?  */
  276.     S_SET_NAME (symbol, NULL);
  277.     symbol->sy_frag = frag_now;
  278.     S_SET_VALUE (symbol, (valueT) frag_now_fix ());
  279.       }
  280.  
  281.     S_SET_TYPE (symbol, type);
  282.     S_SET_OTHER (symbol, other);
  283.     S_SET_DESC (symbol, desc);
  284.   }
  285. #endif /* ! OBJ_PROCESS_STAB */
  286. #endif /* ! SEPARATE_STAB_SECTIONS */
  287.  
  288. #ifndef NO_LISTING
  289.   if (listing)
  290.     {
  291.       switch (type)
  292.     {
  293.     case N_SLINE:
  294.       listing_source_line ((unsigned int) desc);
  295.       break;
  296.     case N_SO:
  297.     case N_SOL:
  298.       listing_source_file (string);
  299.       break;
  300.     }
  301.     }
  302. #endif /* ! NO_LISTING */
  303.  
  304.   demand_empty_rest_of_line ();
  305. }
  306.  
  307. /* Regular stab directive. */
  308.  
  309. void
  310. s_stab (what)
  311.      int what;
  312. {
  313.   s_stab_generic (what, STAB_SECTION_NAME, STAB_STRING_SECTION_NAME);
  314. }
  315.  
  316. /* "Extended stabs", used in Solaris only now. */
  317.  
  318. void
  319. s_xstab (what)
  320.      int what;
  321. {
  322.   int length;
  323.   char *stab_secname, *stabstr_secname;
  324.  
  325.   stab_secname = demand_copy_C_string (&length);
  326.   SKIP_WHITESPACE ();
  327.   if (*input_line_pointer == ',')
  328.     input_line_pointer++;
  329.   else
  330.     {
  331.       as_bad ("comma missing in .xstabs");
  332.       ignore_rest_of_line ();
  333.       return;
  334.     }
  335.  
  336.   /* To get the name of the stab string section, simply add "str" to
  337.      the stab section name.  */
  338.   stabstr_secname = (char *) xmalloc (strlen (stab_secname) + 4);
  339.   strcpy (stabstr_secname, stab_secname);
  340.   strcat (stabstr_secname, "str");
  341.   s_stab_generic (what, stab_secname, stabstr_secname);
  342.   free (stabstr_secname);
  343. }
  344.  
  345. #ifdef S_SET_DESC
  346.  
  347. /* Frob invented at RMS' request. Set the n_desc of a symbol.  */
  348.  
  349. void 
  350. s_desc (ignore)
  351.      int ignore;
  352. {
  353.   char *name;
  354.   char c;
  355.   char *p;
  356.   symbolS *symbolP;
  357.   int temp;
  358.  
  359.   name = input_line_pointer;
  360.   c = get_symbol_end ();
  361.   p = input_line_pointer;
  362.   *p = c;
  363.   SKIP_WHITESPACE ();
  364.   if (*input_line_pointer != ',')
  365.     {
  366.       *p = 0;
  367.       as_bad ("Expected comma after name \"%s\"", name);
  368.       *p = c;
  369.       ignore_rest_of_line ();
  370.     }
  371.   else
  372.     {
  373.       input_line_pointer++;
  374.       temp = get_absolute_expression ();
  375.       *p = 0;
  376.       symbolP = symbol_find_or_make (name);
  377.       *p = c;
  378.       S_SET_DESC (symbolP, temp);
  379.     }
  380.   demand_empty_rest_of_line ();
  381. }                /* s_desc() */
  382.  
  383. #endif /* defined (S_SET_DESC) */
  384.